package top.lyc829.mymusic.ui.custom

import android.animation.ObjectAnimator
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.animation.LinearInterpolator
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModelProvider
import com.lxj.xpopup.XPopup
import com.lzx.starrysky.SongInfo
import com.lzx.starrysky.StarrySky
import com.lzx.starrysky.manager.PlaybackStage
import top.lyc829.mymusic.MyApp
import top.lyc829.mymusic.R
import top.lyc829.mymusic.ext.loadAsCircle
import top.lyc829.mymusic.viewmodel.EventViewModel


/**
 * @Author lyc
 * @create 2021-07-18 15:47
 */
class BottomMusicView @JvmOverloads constructor (
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
): ConstraintLayout(context, attrs, defStyleAttr) {

    companion object {
        const val TAG = "BottomMusicView"
    }

    private lateinit var mRotateAnimator: ObjectAnimator
    private var currentPlayTime: Long = 0

    private var songInfo: SongInfo? = null

    private lateinit var playButton: CircleProgressButton
    private lateinit var albumImage: ImageView
    private lateinit var listButton: ImageView
    private lateinit var audioNameView: TextView
    private lateinit var audioAlbumView: TextView
    private val eventViewModel: EventViewModel by lazy { ViewModelProvider(MyApp.context as MyApp)
        .get(EventViewModel::class.java) }

    init {
        initView()
        callback()
    }

    private fun callback() {
        //观察播放状态
        eventViewModel.playBackStage.observe(context as LifecycleOwner) {
            if (it.songInfo != songInfo) {
                songInfo = it.songInfo
                //更新界面
                updateView()
            }
            if (it.stage == PlaybackStage.PLAYING) {
                mRotateAnimator.start()
                mRotateAnimator.currentPlayTime = currentPlayTime
            } else {
                currentPlayTime = mRotateAnimator.currentPlayTime
                mRotateAnimator.cancel()

            }
        }
    }

    /**
     * 更新界面
     */
    private fun updateView() {
        songInfo?.let {
            albumImage.loadAsCircle(it.songCover)
            audioNameView.text = it.songName
            audioAlbumView.text = it.artist
        }
    }

    private fun initView() {
        val rootView: View = LayoutInflater.from(context).inflate(R.layout.bottom_music_view, this)
        playButton = rootView.findViewById(R.id.play_view)
        albumImage = rootView.findViewById(R.id.album_view)
        listButton = rootView.findViewById(R.id.show_list_view)
        audioNameView = rootView.findViewById(R.id.audio_name_view)
        audioAlbumView = rootView.findViewById(R.id.audio_album_view)

        // 根布局点击事件
        rootView.setOnClickListener {
        }
        //属性动画 旋转动画
        mRotateAnimator = ObjectAnimator.ofFloat(albumImage, ROTATION.name, 0f, 360f)
        mRotateAnimator.duration = 10000
        mRotateAnimator.interpolator = LinearInterpolator()
        mRotateAnimator.repeatMode = ObjectAnimator.RESTART // 循环模式
        mRotateAnimator.repeatCount = ObjectAnimator.INFINITE

        listButton.setOnClickListener {
            if (StarrySky.with().getPlayList().isEmpty()) {
                Toast.makeText(context, "当前播放队列没有歌曲", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            XPopup.Builder(context)
                .asCustom(MusicListDialog(context))
                .show()

        }

        //TODO: 显示上次退出时播放的歌曲
    }

    override fun onWindowFocusChanged(hasWindowFocus: Boolean) {
        super.onWindowFocusChanged(hasWindowFocus)
        if (StarrySky.with().isPlaying()) {
            when (hasWindowFocus) {
                true -> {
                    mRotateAnimator.start()
                    mRotateAnimator.currentPlayTime = currentPlayTime
                }
                else -> {
                    currentPlayTime = mRotateAnimator.currentPlayTime
                    mRotateAnimator.cancel()
                }
            }

        }
    }
}